package com.bbs.dao.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.bbs.dao.BaseDao;
import com.bbs.dao.TopicDao;
import com.bbs.model.Topic;

public class TopicDaoImpl extends BaseDao  implements TopicDao  {

	public TopicDaoImpl() throws IOException {
		super();
	}

	public int save(Topic t) {
		int r=sqlSession.insert("com.bbs.model.Topic.insert",t);
		sqlSession.commit();
		sqlSession.close();
		return r;
	}

	public int delete(int id) {
		int r=sqlSession.delete("com.bbs.model.Topic.delete",id);
		sqlSession.commit();
		sqlSession.close();
		return r;
	}

	public int update(Topic t) {
		int r=sqlSession.update("com.bbs.model.Topic.update",t);
		sqlSession.commit();
		sqlSession.close();
		return r;
	}

	public Topic findById(int id) {
		Topic topic=sqlSession.selectOne("com.bbs.model.Topic.findById",id);
		//sqlSession.commit();
		sqlSession.close();
		return topic;
	}

	@Override
	public List<Topic> findTopics(int start, int end) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("start", start);
		map.put("end",end);
		return sqlSession.selectList("com.bbs.model.Topic.findTopics",map);
	}
	public List<Topic> getTopicList() {
		List<Topic> topicList;
		topicList=sqlSession.selectList("com.bbs.model.Topic.getTopicList");
		return  topicList;
	}

	@Override
	public List<Topic> getTopicListBySectionId(int section_id) {
		List<Topic> topicList;
		topicList=sqlSession.selectList("com.bbs.model.Topic.findTopicsBySectionId",section_id);
		return  topicList;
	}

	public void addClic_count(int id) {
		sqlSession.update("com.bbs.model.Topic.addClick_count",id);
		sqlSession.commit();
		sqlSession.close();
		
	}

}
